# !/user/bin.env python3
# -*- coding: utf-8 -*-

import os
import unittest
import time
from appium import webdriver
from time import sleep



# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

class ContactsAndroidTests(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '8.0.0'
        desired_caps['deviceName'] = '10.0.4.120:5555'
        desired_caps['app'] = PATH('/Users/aihelp/androidspace/app/release/appium-release.apk')
        desired_caps['appPackage'] = 'com.aihelp.appium'
        desired_caps['appActivity'] = '.MainActivity'


        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)




    # def tearDown(self):
    #     self.driver.quit()

    def test_add_contacts(self):
        bt =self.driver.find_element_by_id('edit_appium')
        bt.click()
        print ('点击按钮了')

        # alert = self.driver.switch_to.Alert
        # alert = self.driver.switch_to_alert(self)
        et = self.driver.find_element_by_class_name("android.widget.EditText")
        et.send_keys('a')

        self.assertEqual('a', et.text)

        # for some reason "save" breaks things
        # alert = self.driver.switch_to_alert()

        # no way to handle alerts in Android
        # self.driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)').click()
        #



if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(ContactsAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)
